package com.jarvis.user.controller;

import java.util.Arrays;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jarvis.user.entity.UserEntity;
import com.jarvis.user.service.UserService;
import com.jarvis.common.utils.PageUtils;
import com.jarvis.common.utils.R;



/**
 * 用户表
 *
 * @author gongxin
 * @email 19910320678@163.com
 * @date 2023-03-31 10:56:20
 */
@RestController
@RequestMapping("user/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestHeader Map<String, String> headers){

        System.out.println(headers);
        String deviceId = headers.get("deviceid");
        UserEntity user =  userService.login(deviceId);
        if(user != null) {
            return R.ok().put("data",user);
        }else {
            return R.error(500,"登录失败");
        }

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
  //  @RequiresPermissions("user:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("user:user:info")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("user:user:save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("user:user:update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("user:user:delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
